OP2 network info



Each packet starts with a generic header. Depending on the type of packet, the payload section is interpreted differently. 

GenericPacketHeader
-------------------
Offset 	Size	Description
------ 	----	-----------
0x0	4	sourcePlayerNetID
0x4	4	destPlayerNetID  [0 for BroadcastToAllPlayers]
0x8	1	sizeOfPayload
0x9	1	type (1 = CommandPacket, other = ProtocolControl)
0xA	4	checksum
-------------------
0xE	X	Payload


If the Size of payload field is less than 0x12 (18 bytes) the packet is immediately discarded.
If the Size of payload field + 0xE (14 bytes) is greater than the size of the received packet, the packet is discarded. (Note: the header takes 14 bytes).
If the Checksum is wrong the packet is discarded.
If the Type field is not 1, the packet is processed like a protocol control command.
If the Type field is 1, the game stores the buffer on a queue for later processing by the game engine. (CommandPacket)





I've never really bothered to figure out the structure of protocol control packets.

ProtocolControl payload
---------------
Size	Description
----	-----------
4	commandType  [Values from 1-6]
		1 = [TellHostUpdatedStatus]
		2 = [Unprocessed]  [Unused]
		3 = [Unprocessed]  [Sent to self as GameData]  [processed elsewhere]
		4 = [SetPlayerList]
		5 = [???]  [Sent to self as 4 as GameData]
		6 = [UpdatePlayerStatus]
----
X	Variable data fields  [See below]


commandType = 1 [TellHostUpdatedStatus]
---------------
---------------  [No data fields]  [Wrong?]
4	int updatedStatus


commandType = 3 []  
[Sent to self after SetPlayerList update]
---------------
---------------  [No data fields]


commandType = 4 [SetPlayerList]
---------------
4	numPlayers			[Update Player Count]
X	playerNetworkAddressList[]	[Update Address List]
---------------


commandType = 5 []
[Causes status udate to 4, and resends to self as commandType = 4]
---------------
???
---------------


commandType = 6 [UpdatePlayerStatus]
---------------
2	status  [Only updates if new status is greater]
---------------




Data packets are used while a game is in play. The payload of data packets consist of a header describing the remainder of the data payload. If the packet contains player commands, the header is followed by a number of data blocks, each of which is prefixed by a 6 byte header.

GamePlayPacketHeader
--------------------
Offset 	Size	Description
------ 	----	-----------
0xE	4	commandType (1-6)		**Figure these out**
0x12	1	type				**Player quit, resync, player commands**
----------------
0x13	1	sourcePlayerNum
0x14	4	tick
----------------
0x18	X	Sequence of Data blocks



CommandType
-----------
0	GamePlayPacket
1	ResendRequest?
2	(unused)
3	(unused)
4	ReplicatedPlayersList
5	
6	

8


The Type field controls the remainder of the packet. Undefined types produce an "unknown message" debug string. The following values are defined:
Value	Description
-----	-----------
0xB	Player quitting
0xC	Player gameplay commands
0xD	TM_RESYNC_MASTER
0xE	Bogus TM_ACK_RESYNC_MASTER
0xF	Bogus TM_SET_NEW_STATE

If a player lags out, the Resync messages are used to continue the game with the remaining players. A TM_RESYNC_MASTER message is sent by the player doing the kicking, to the other players. The player who initiates the Resync becomes known as the resync master. The clients then send "TICK data" for each player to the master (each client sends data for all players). The clients then receive "TICK data" from the master and get a TM_SET_NEW_STATE message.




For player gameplay commands (Type is 0xC) the remainder of the data lies in a number of data blocks. The game loops and copies each data block (and expands some header fields slightly) into buffers for processing by the game engine (presumably).

Data block header
-----------------
Size	Description
----	-----------
1	Type of data block
1	Size of data block
4	Unknown				**Figure this out**
-------------------
X	Data block payload




For chat messages between players, the Type is set to 0x30. The data block payload then consists of
Data Block Payload
------------------
Size	Description
----	-----------
1	Player number of source (player num on Data payload header is not copied into game engine)
1	Send to flags (bit 'i' is set if player number 'i' is to receive the message)
X	ASCII text message

Type 0x0 with length 0x0 are used to when no commands are being sent. It just causes the games to update the time of the last received message.

Type 0x34 seems to always have length 0x8. 		**Figure out what it means**























Network Game Search Broadcast packet
------------------------------------
This is the broadcast packet used to look for network games on a LAN. (or specific IP address)
Replies to the broadcast are listened to on the first port available in the range 47800-47807.
packet send routine	: 0x004917E0
packet received routine	: 0x0048C2C0
source port		: (random)
destination port	: 47776
packet data length	: 0x2A (42)

Class Offset	Buffer Offset 	Data Size 	Description
------------	-------------	---------	-----------
0x24		0x0		4		Packet Checksum (sum of DWORDs + WORD XORed with 0xFDE24ACB)
0x28		0x4		2		Packet Type (0x1000)
- - - - - - - - - - - - - - - - - - - - -
0x2A		0x6		16		Identifier (a GUID maybe?)
- - - - - - - - - - - - - - - - - - - - -
0x3A		0x16		4		Port Number for return traffic (in range 47800-47807, first available for binding)
0x3E		0x1A		4		Time Stamp (WINMM.timeGetTime)
- - - - - - - - - - - - - - - - - - - - -
0x42		0x1E		0xB		Game password (NULL terminated string)
0x4D		0x29		1		0x0 (NULL terminator, forced max length)
-----------------------------------------


Network Game Broadcast Reply packet
-----------------------------------
This packet is used by a game host to respond to client braodcasts for network games on a LAN. (or specific IP address)
packet send routine	: 0x0048C2C0
packet received routine	: 0x00491A50  (Thread entry point)
source port		: (random)
destination port	: 47800-47807 (Port number read from Network Game Search Broadcast packet)
packet data length	: 0x51 (81)

Class Offset 	Buffer Offset 	Data Size 	Description
------------	-------------	---------	-----------
(0x58)		0x0		4		Packet Checksum
		0x4		2		Packet Type (0x1001)
- - - - - - - - - - - - - - - - - - - - -
		0x6		4		Time Stamp (copied from received packet)
- - - - - - - - - - - - - - - - - - - - -
		0xA		16		Identifier - Program - (a GUID maybe?)
		0x1A		16		Identifier - Game session identifier - GUID (ole32.CoCreateGuid)
- - - - - - - - - - - - - - - - - - - - -
		0x2A		4		Game type bitfield
		  0-0		  1 bit		  0 Unused? Wrong -> Disasters (0 = No, 1 = Yes)
		  1-1		  1 bit		  0 Unused? Wrong -> Day and Night (0 = No, 1 = Yes)
		  2-2		  1 bit		  0 Unused? Wrong -> Morale (0 = No, 1 = Yes)
		  3-3		  1 bit		  0 Unused? 
		  4-4		  1 bit		  0 Unused? 
		  5-5		  1 bit	  	  0 Unused? 
		  6-8	(0x1C0)	  3 bits	  Max players (2-6)
		  9-13	(0x3E00)  5 bits	  Game type (0/24 = LastOne, 1/25 = Midas, 2/26 = ResourceRace, 3/27 = Space Race, 4/28 = Land Rush)
		  14-31		  18 bits	  0x0 Unused?
		0x2E		4		
		0x32		4		
		0x36		4		First 4 letters of the session creator's name
- - - - - - - - - - - - - - - - - - - - -
		0x3A		4		Max Players
		0x3E		4		(0x0)
- - - - - - - - - - - - - - - - - - - - -
		0x42		14 (0xE)	Session creator name (NULL terminated string)
		0x50		1		0x0 (NULL terminator, forced max length)
-----------------------------------------



Network Game Join Request packet
--------------------------------
This packet is sent by the client to the game host to request to join a game.
Note: This data format apears to be the first format described in this file.
packet send routine	: 0x004965A0
packet received routine	: 
source port		: 
destination port	: 47777
packet data length	: 0x32 (50)

Stack Offset 	Buffer Offset 	Data Size 	Description
------------	-------------	---------	-----------
0x4C		0x0		4		PlayerNetID
0x50		0x4		4		Unknown **TODO** find out
0x54		0x8		1		Size of Payload (0x24)
0x55		0x9		1		Type of Packet (0x1)
0x56		0xA		4		Checksum
- - - - - - - - - - - - - - - - - - - - -
0x5A		0xE		4		0x0 (???)
0x5E		0x12		16		GUID?
0x6E		0x22		4		Port number for returned data (47800-47807)
0x72		0x26		11 (0xB)	Session password (NULL terminated string)
0x7D		0x31		1		0x0 (NULL terminator, forced max length)
-----------------------------------------



Network Game Join Response packet
---------------------------------
This packet is sent from the game host to the client upon the client's request to 
join the session.
packet send routine	: 
packet received routine	: 0x004965A0
source port		: 
destination port	: 
packet data length	: 0x26 (38)

Stack Offset 	Buffer Offset 	Data Size 	Description
------------	-------------	---------	-----------
0x14		0x0		4		PlayerNetID
0x18		0x4		4		Unknown (???)
0x1C		0x8		1		Size of Payload (0x18)
0x1D		0x9		1		Type of Packet (0x1)
0x1E		0xA		4		Checksum
- - - - - - - - - - - - - - - - - - - - -
0x22		0xE		4		Join Status (0x1 = Granted, 0x2 = Rejected) ?
0x26		0x12		16		GUID? (Note: must match the GUID value in the Game Join Request packet or the response is discarded)
0x36		0x22		4		Bitfields ???
		  0-2		  3 bits	  
-----------------------------------------



Game Settings Request packet ???
----------------------------
This packet is sent to the game host by the client after the host has acknowledged
a join request with JoinStatus := Granted.
packet send routine	: 0x00497190
packet received routine	: 
source port		: 
destination port	: 
packet data length	: 

Stack Offset 	Buffer Offset 	Data Size 	Description
------------	-------------	---------	-----------
0x8		0x0		4		???
0xC		0x4		4		???
0x10		0x8		1		Size of Payload (0x6)
0x11		0x9		1		Type of Packet (0x1)
0x12		0xA		4		Checksum
- - - - - - - - - - - - - - - - - - - - -
0x16		0xE		4		??? (0x6)
0x1A		0x12		2		(sample value 0x2) ???
-----------------------------------------



